package cn.boge.hrm.controller;

import cn.boge.hrm.dto.SendSmsCodeDTO;
import cn.boge.hrm.exception.BusinessException;
import cn.boge.hrm.service.IVerifycodeService;
import cn.boge.hrm.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/verifycode")
public class VerifycodeController {
    @Autowired
    private IVerifycodeService iVerifycodeService;
    /**
     * 获取验证码图形
     * @param key
     * @return
     */
    @GetMapping("/imageCode/{key}")
    public AjaxResult imageCode(@PathVariable("key")String key){
        String base64 = iVerifycodeService.imageCode(key);
        return new AjaxResult().setResultObj(base64);
    }

    /**
     * 获取短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody @Valid SendSmsCodeDTO dto) {

        try {
            iVerifycodeService.sendSmsCode(dto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        }
    }
}
